
import {requestLogin} from '../../service/login';

import {message} from 'antd';

import {routerRedux} from "dva/router";  //路由跳转

let authTool  = require('../../tools/auth-tool');

export default {
  namespace: 'login',
  state:{
    logining:false,
  },

  effects:{
    *login({payload}, {call, put}){
      const {username, password}  = payload;
      const result  = yield call(requestLogin, username, password);
      const {status, data}  = result;
      // Login success
      if(status == 200){
        const {token, roles}  = data;
        console.log("Login success. Token is", token, "role is", roles);

        authTool.clear();

        authTool.setToken(token);

        for(let role  of roles){
          authTool.addRole(role.roleName);
        }

        authTool.storeAuthData();

        yield put(routerRedux.push('/workspace/reception'));
        yield put({type:'project/queryProjectInfos'});
      }
      else{
        authTool.clear();
        message.error('登录失败:' + data);
      }
    },
  }
}
